<template>
  <el-dialog :title="isEdit ? '修改品牌类型': '新增品牌类型'" :visible.sync="visible" :close-on-click-modal="closeOnClickModal" @open="_handleDialogOpen" @close="_cancel">
    <el-form ref="dataForm" :model="form" :status-icon="true" :rules="formRules" label-width="100px">
      <el-form-item v-show="!!parentData" label="上级名称">
        <span>{{ parentData ? parentData.name : '' }}</span>
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input v-model="form.name" placeholder="请输入名称"/>
      </el-form-item>
      <el-form-item label="排序号" prop="sortNo">
        <el-input-number v-model="form.sortNo" placeholder="请输入排序号"/>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button size="small" @click="_cancel">取 消</el-button>
      <el-button :loading="_loading" size="small" type="primary" @click="_ok">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getBrandById } from '@/api/base/brand'
export default {

  props: {
    closeOnClickModal: {
      type: Boolean,
      default: false
    },

    loading: {
      type: Boolean,
      default: false
    }
  },

  data() {
    return {
      iconList: [],
      parentData: null,
      isEdit: false,
      visible: false,
      form: {},
      formRules: {
        name: [{
          required: true,
          message: '请输入名称'
        }]
      }
    }
  },
  computed: {
    '_loading': {
      get() {
        return this.loading
      },

      set(value) {
        this.$emit('update:loading', value)
      }
    }
  },
  created() {
  },
  methods: {
    _formReset() {
      this.form = {
        name: '',
        sortNo: 1
      }
      this.$nextTick(() => {
        this.$refs.dataForm.resetFields()
      })
    },

    _handleDialogOpen() {
      this._formReset()
      this.$emit('open')
    },

    _cancel() {
      this._formReset()
      this.visible = false
      this.isLoading = false
      this.$emit('cancel')
    },

    _ok() {
      const form = this.$refs['dataForm']
      form.validate(valid => {
        if (valid) {
          const formData = JSON.parse(JSON.stringify(this.form))

          this.$emit('ok', formData, this.isEdit)
        }
      })
    },

    show(params, parent) {
      this.visible = true
      this.parentData = parent
      if (params) {
        this.isEdit = true
        getBrandById({
          id: params.id
        }).then(resp => {
          this.form = resp.data
        }).catch(resp => {
          this.$message.error(resp.message)
        })
      } else {
        // pass
        this.isEdit = false
        // this._clear()
      }
    },

    close() {
      this._cancel()
    }
  }
}
</script>
<style lang="css" scoped>
  .icon-sel {
    width: 2.2em;
    height: 2.2em;
    margin-left: 10px;
  }
</style>
